package com.hsylient.javase.chapter11.practice;

/**
 * 模拟多个线程去买票
 *
 * 存在文件出现一张票多买的问题
 * 多个线程操作同一个资源的时候，出现并发问题
 */
public class SellingTickets implements Runnable {

    private Integer ticketNumber = 10;

    @Override
    public void run() {
        for (;;) {
            if (ticketNumber < 0) {
                break;
            }
            System.out.println(Thread.currentThread().getName() + "买到第" + ticketNumber-- + "张票");
        }
    }

    public static void main(String[] args) {
        SellingTickets sellingTickets = new SellingTickets();
        new Thread(sellingTickets, "小明").start();
        new Thread(sellingTickets, "老师").start();
        new Thread(sellingTickets, "黄牛").start();
    }
}
